using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.User;
using CE.PersonalAccident;
using CE.Global;

public partial class personal_accident_personal_accident : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            //Session[CE.Global.General.RequestUrl] = "~/home_refinancing/required_loan.aspx";
            //Response.Redirect("~/User/LogInAndRegister.aspx");
        }
        else
        {
            if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) ==
                Convert.ToInt32(CE.Global.CEUserType.Customer))
            {
                ShowUserDetails();
            }
            //txtFullName.Text = Session[CE.Global.General.UserName].ToString();
            //txtMobilePhoneNo.PhoneNo = Session[CE.Global.General.UserPhoneNo].ToString();
            //txtEmailAddress.Text = Session[CE.Global.General.UserEmail].ToString();
        }

        if (!Page.IsPostBack)
        {
            usrDOB.StartYear = CE.Global.General.GetUTCDateTime().Year - 75;
            usrDOB.EndYear = CE.Global.General.GetUTCDateTime().Year - 18;

            usrSPDOB.StartYear = CE.Global.General.GetUTCDateTime().Year - 75;
            usrSPDOB.EndYear = CE.Global.General.GetUTCDateTime().Year - 18;

            usrInsCommentDate.IsNotLessThanToday = true;
            usrInsCommentDate.StartYear = CE.Global.General.GetUTCDateTime().Year;
            usrInsCommentDate.EndYear = CE.Global.General.GetUTCDateTime().Year + 2;

            usrInsCommentDate.Day = CE.Global.General.GetUTCDateTime().Day;
            usrInsCommentDate.Month = CE.Global.General.GetUTCDateTime().Month;
            usrInsCommentDate.Year = CE.Global.General.GetUTCDateTime().Year;

            usrSPDOB.IsEnableValidation(false);
            usrSPOccupation.IsEnableValidation(false);

            ddlPACover.Attributes.Add("onblur", "ShowValidationEffect('" + ddlPACover.ClientID + "','" + rfvPACover.ClientID + "');");
            ddlDeathAmount.Attributes.Add("onblur", "ShowValidationEffect('" + ddlDeathAmount.ClientID + "','" + rfvDeathAmount.ClientID + "');");
            ddlNoOfChild.Attributes.Add("onblur", "ShowValidationEffect('" + ddlNoOfChild.ClientID + "','" + rfvNoOfChild.ClientID + "');");
            //txtOwnerName.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtOwnerName.ClientID + "','" + rfvOwnerName.ClientID + "','" + revOwnerName.ClientID + "');");
            //txtOwnerEmailAddress.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtOwnerEmailAddress.ClientID + "','" + rfvOwnerEmailAddress.ClientID + "','" + revOwnerEmailAddress.ClientID + "');");
            //txtFullName.Attributes.Add("onblur", "ShowValidationEffect('" + txtFullName.ClientID + "','" + rfvFullName.ClientID + "');");
            //txtEmailAddress.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtEmailAddress.ClientID + "','" + rfvEmailAddress.ClientID + "','" + revEmailAddress.ClientID + "');");
            //ddlAnnualIncome.Attributes.Add("onblur", "ShowValidationEffect('" + ddlAnnualIncome.ClientID + "','" + rfvAnnualIncome.ClientID + "');");
            //ddlIncomeType.Attributes.Add("onblur", "ShowValidationEffect('" + ddlIncomeType.ClientID + "','" + rfvIncomeType.ClientID + "');");

            //ClearSession();
            if (Session[General.PAId] != null)
            {
                ShowPADetails();
            }
            General.ClearAllSession(this.Page, "PA");
            Session[General.ViewedDetailsId] = LogUserViewedHistory().ToString();
        }
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        usrInsCommentDate.IsEnableValidation(true);
        if (ddlPACover.Text == "INDCHD" && ddlPACover.Text == "INDSPCHD")
        {
            rfvNoOfChild.Enabled = true;
        }
        else
        {
            rfvNoOfChild.Enabled = false;
        }

        if (ddlPACover.Text == "INDSP" && ddlPACover.Text == "INDSPCHD")
        {
            usrSPDOB.IsEnableValidation(true);
            usrSPOccupation.IsEnableValidation(true);
        }
        else
        {
            usrSPDOB.IsEnableValidation(false);
            usrSPOccupation.IsEnableValidation(false);
        }

        ChangeValidationEffectServerSide("personal");

        if (Page.IsValid)
        {
            PersonalAccident _personalacc = new PersonalAccident();
            if (Session[General.PAId] != null)
            {
                _personalacc.PAID = Convert.ToInt64(Session[General.PAId].ToString());
            }
            else
            {
                _personalacc.PAID = 0;
            }
            _personalacc.CoverType = ddlPACover.Text;
            _personalacc.DeathAmount = Convert.ToSingle(ddlDeathAmount.Text);
            _personalacc.DOB = usrDOB.GetDate();
            _personalacc.Occupation = usrOccupation.GetOccupation();

            if (ddlPACover.Text == "INDCHD" || ddlPACover.Text == "INDSPCHD")
            {
                _personalacc.NoOfChild = Convert.ToInt16(ddlNoOfChild.Text);
            }
            else
            {
                _personalacc.NoOfChild = 0;
            }

            if (ddlPACover.Text == "INDSP" || ddlPACover.Text == "INDSPCHD")
            {
                _personalacc.SPDOB = usrSPDOB.GetDate();
                _personalacc.SPOccupation = usrSPOccupation.GetOccupation();
            }
            else
            {
                _personalacc.SPDOB = CE.Global.General.GetUTCDateTime();
                _personalacc.SPOccupation = "NONE";
            }

            _personalacc.IsAcceptTerm = true;
            if (Session[General.UserId] != null)
            {
                _personalacc.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());
            }
            else
            {
                _personalacc.RefCEUserId = 0;
            }
            //Start - Comment on 08/06/2010 Sakthi
            //_personalacc.SurName = txtOwnerName.Text;
            //_personalacc.Email = txtOwnerEmailAddress.Text;
            //_personalacc.PhoneNo = txtOwnerPhoneNo.GetPhoneNo();
            //End
            _personalacc.ViewedDetailId = Convert.ToInt64(Session[General.ViewedDetailsId].ToString());
            _personalacc.Status = true;

            PersonalAccidentDB _personalaccdb = new PersonalAccidentDB();
            Int64 _result = _personalaccdb.Insert(_personalacc);
            if (_result > 0)
            {
                Session[General.PAId] = _result.ToString();
                //Start - Comment on 08/06/2010 Sakthi
                //Session[General.PAPhoneNo] = txtOwnerPhoneNo.GetPhoneNo();
                //Session[General.PAUserName] = txtOwnerName.Text;
                //Session[General.PAEmail] = txtOwnerEmailAddress.Text;
                //End
                //Session[General.PAPhoneNo] = txtMobilePhoneNo.GetPhoneNo();
                //Session[General.PAUserName] = txtFullName.Text;
                //Session[General.PAEmail] = txtEmailAddress.Text;
                //Response.Write("javascript:alert('save successfully!');");
                Response.Redirect("PAResultsQuotes.aspx");
            }
        }
    }

    private void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        //Type of Personal Accident Cover
        if (rfvPACover.Enabled && rfvPACover.ValidationGroup == strValidationGroup)
        {
            rfvPACover.Validate();
            if (rfvPACover.IsValid == false)
            {
                ddlPACover.ControlStyle.CssClass = "validationError";
            }
            else
            {
                ddlPACover.ControlStyle.CssClass = "";
            }
        }
        else
        {
            ddlPACover.ControlStyle.CssClass = "";
        }

        //Amount of Accidental Death Protection
        if (rfvDeathAmount.Enabled && rfvDeathAmount.ValidationGroup == strValidationGroup)
        {
            rfvDeathAmount.Validate();
            if (rfvDeathAmount.IsValid == false)
            {
                ddlDeathAmount.ControlStyle.CssClass = "validationError";
            }
            else
            {
                ddlDeathAmount.ControlStyle.CssClass = "";
            }
        }
        else
        {
            ddlDeathAmount.ControlStyle.CssClass = "";
        }

        //Date of Birth
        usrDOB.ChangeValidationEffectServerSide(strValidationGroup);

        //Occupation
        usrOccupation.ChangeValidationEffectServerSide(strValidationGroup);

        //Insurance Commence Date
        usrInsCommentDate.ChangeValidationEffectServerSide(strValidationGroup);

        //Name
        //Start - Comment on 08/06/2010 Sakthi
        /*if (rfvOwnerName.Enabled && rfvOwnerName.ValidationGroup == strValidationGroup)
        {
            rfvOwnerName.Validate();
            if (rfvOwnerName.IsValid == false)
            {
                txtOwnerName.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtOwnerName.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtOwnerName.ControlStyle.CssClass = "";
        }

        //Phone Number
        txtOwnerPhoneNo.ChangeValidationEffectServerSide(strValidationGroup);

        //Email Address
        if (rfvOwnerEmailAddress.Enabled && rfvOwnerEmailAddress.ValidationGroup == strValidationGroup)
        {
            rfvOwnerEmailAddress.Validate();
            if (rfvOwnerEmailAddress.IsValid == false)
            {
                txtOwnerEmailAddress.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtOwnerEmailAddress.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtOwnerEmailAddress.ControlStyle.CssClass = "";
        }

        if (revOwnerEmailAddress.Enabled && revOwnerEmailAddress.ValidationGroup == strValidationGroup)
        {
            if (rfvOwnerEmailAddress.IsValid)
            {
                revOwnerEmailAddress.Validate();
                if (revOwnerEmailAddress.IsValid == false)
                {
                    txtOwnerEmailAddress.ControlStyle.CssClass = "validationError";
                }
                else
                {
                    txtOwnerEmailAddress.ControlStyle.CssClass = "";
                }
            }
        }
        else
        {
            txtOwnerEmailAddress.ControlStyle.CssClass = "";
        }
        */
        //End
    }

    private Int64 LogUserViewedHistory()
    {
        UserViewHistoryDB _userviewdb = new UserViewHistoryDB();
        return _userviewdb.LogViewedHistory("PA", Request, "Personal Accident quote.");
    }

    private void ShowPADetails()
    {
        PersonalAccidentDB _paaccdb = new PersonalAccidentDB();
        PersonalAccident _paacc = new PersonalAccident();
        _paacc = _paaccdb.GetPersonalAccident(Convert.ToInt64(Session[General.PAId].ToString()));
        if(_paacc != null)
        {
            ddlPACover.Text = _paacc.CoverType;
            ddlDeathAmount.Text = Convert.ToInt32(_paacc.DeathAmount).ToString();
            
            usrDOB.Day = _paacc.DOB.Day;
            usrDOB.Month = _paacc.DOB.Month;
            usrDOB.Year = _paacc.DOB.Year;

            usrOccupation.Occupation = _paacc.Occupation;
            usrInsCommentDate.Day = _paacc.InsCommentDate.Day;
            usrInsCommentDate.Month = _paacc.InsCommentDate.Month;
            usrInsCommentDate.Year = _paacc.InsCommentDate.Year;

            if (_paacc.CoverType == "INDCHD" || _paacc.CoverType == "INDSPCHD")
            {
                ddlNoOfChild.Text = _paacc.NoOfChild.ToString();
            }
            else
            {
                ddlNoOfChild.Text = "-1";
            }

            if (_paacc.CoverType == "INDSP" || _paacc.CoverType == "INDSPCHD")
            {
                usrSPDOB.Day = _paacc.SPDOB.Day;
                usrSPDOB.Month = _paacc.SPDOB.Month;
                usrSPDOB.Year = _paacc.SPDOB.Year;

                usrSPOccupation.Occupation = _paacc.SPOccupation;
            }

            if (Session[CE.Global.General.UserId] == null && Convert.ToInt32(Session[CE.Global.General.UserTypeId]) !=
                Convert.ToInt32(CE.Global.CEUserType.Customer))
            {
                //Start - Comment on 08/06/2010 Sakthi
                //txtOwnerName.Text = _paacc.SurName;
                //txtOwnerPhoneNo.PhoneNo = _paacc.PhoneNo;
                //txtOwnerEmailAddress.Text = _paacc.Email;
                //End
            }
        }
    }

    private void ShowUserDetails()
    {
        //Start - Comment on 08/06/2010 Sakthi
        //hplEditProfile.Visible = true;

        //txtOwnerName.Text = Session[CE.Global.General.UserName].ToString();
        //txtOwnerPhoneNo.PhoneNo = Session[CE.Global.General.UserPhoneNo].ToString();
        //txtOwnerEmailAddress.Text = Session[CE.Global.General.UserEmail].ToString();

        //txtOwnerName.ReadOnly = true;
        //txtOwnerPhoneNo.IsReadOnly = true;
        //txtOwnerEmailAddress.ReadOnly = true;
        //End
    }

    private void ClearSession()
    {
        Session[General.PAId] = null;
        Session[General.PAPhoneNo] = null;
        Session[General.PAEmail] = null;
        Session[General.PAUserName] = null;
    }
}
